/**********************************************************
 * display.c - contains the code for the header file
 * display.h
 *
 * By: Team 4
 * *******************************************************/

#include "msp430bt5190.h"
#include "./Includes/display.h"
#include<stdio.h>
#include "./Includes/GSGCdef.h"
//#include "GlobalVars.h"
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>


// Global variables
extern const Semaphore_Handle UART0Sem;    //posted when a recieve happens from UART0

/**********************************************************
 * Helper Functions - functions called by other functions
 * in the header that help with the implementation.
 *******************************************************/


// Sets-up UART communications for the OLED

int UART_OLED_init(){
  // Setting up the UART Communications
  //Setting UCSWRST
  UCA0CTL1 |= UCSWRST; //Start reconfiguring by setting UCSWRST bit
  
  //Initializing Registers
  UCA0CTL0 &= ~UCMSB; //selects LSB first
  UCA0CTL1 = UCSSEL1; // UCLK = ACLK = 1Hz
  UCA0BR0 = 0x68; // 1MHz/9600 = 104
  UCA0BR1 = 0x00; // BR0(low byte) BR1(high byte)
  UCA0MCTL = 0x00; // Modulation
  //UCA0CTL1 |= UCRXEIE; // Enable UART0 RX/TX interrupt
  //UCA0IFG &= ~UCRXIFG; // set the RX flag low
  //UCA0IFG &= ~UCTXIFG; // set the TX flag low
  
  // Configuring Ports
  P3SEL |= 0x30; // P3.4,5 = UART0 TXD/RXD
  
  //Clearing UCSWRST
  UCA0CTL1 &= ~UCSWRST; //Initialize USART state machine
  
  //Setting Interrupts on the receive signal
  UCA0IE |= UCRXIE;
  
  return 0;
}

/**********************************************************
 * Transmit functions - allow for communication along the
 * UART
 * *******************************************************/

// Waits till an ACK or NACK is received
// blocking function
int waitACK(){
  int result;
  // Wait till there is a new value in the receive buffer
  //UCA0IFG &= ~UCRXIFG;
  //while(!(UCA0IFG & UCRXIFG));
  //if(!Semaphore_pend(UART0Sem,BIOS_NO_WAIT)) //clear current semaphore or breaks immediately
  if(!Semaphore_pend(UART0Sem,10))
	  return 2;//Timed out error

  // see if the buffer has an ACK, NAK or unknown value
  if(UCA0RXBUF == ACK){
    result = 0;
  }else if(UCA0RXBUF == NAK){
    result = 1;
  }else{
    result = 2;
  }

  // reset the flag
  //UCA0IFG &= ~UCRXIFG; // set the RX flag low
  return result;
}

// checks if new value is in buffer and if so returns
// based on if it is an ACK, NAK or unknown
// non-blocking function
int checkACK(){
  int result;

  //check if there is a new vaule in the buffer
  if((UCA0IFG & UCRXIFG)){   
	  // see if the buffer has an ACK, NAK or unknown value
	  if(UCA0RXBUF == ACK){
	    result = 0;
	  }else if(UCA0RXBUF == NAK){
	    result = 1;
	  }else{
	    result = 2;
	  }
	
	  // reset the flag
	  UCA0IFG &= ~UCRXIFG; // set the RX flag low
  } else {
    result = -1;
  }

  return result;
}

// Two transmit functions for sending information out the
// UART repeating the transmissions until an ACK is recieved

// this one sends a string that has num of elements
int transmit_string(char * string, int num){
int i,j;
int RX;
volatile int count = 0;
const int max = 10;
  for(j=0; j < max; j++){
    //for(count = 0; count < 5000; count++){}
    for(i = 0; i < num; i++){
      while(!(UCA0IFG & UCTXIFG));  //Wait till buffer is ready
      UCA0TXBUF = string[i];   //Send next command
    }
    
    RX = waitACK();
    if(RX == 0) //Saw an ACK so break
      break;
  }
  if(j == max)
	  return 1;
  else
	  return 0;
}

// This one sends a signle character
int transmit_char(char val){
int RX;
  for(;;){
    while(!(UCA0IFG & UCTXIFG));  //Wait till buffer is ready
    UCA0TXBUF = val;

    RX = waitACK();
    if(RX == 0) //Saw an ACK so break
      break;
  }
  return 0;
}

// Sends a repeated auto baud command (Ox55) times amount of 
// times if times = 0 it will loop until it receives an ack 
// signal returns 0 if ACK is received and 1 if runs out of 
// time

int send_auto_baud(int times){
  int i;
  int RX;

  for(i = 0; (i < times)|(times == 0); i++){
    while(!(UCA0IFG & UCTXIFG));  //Wait till buffer is ready
    UCA0TXBUF = GSGC_AUTOBAUD;

    RX = checkACK();
    if(RX == 0) // an ACK was seen so break
      break;
  }
  return(1); 
}

/**********************************************************
 * Display Functions - functions that are used to change the
 * display's background
 *******************************************************/

// Change the background color
int set_background(char R, char G, char B){
  // Set up mask for transmission
  const char Rmask = 0xF8;
  const char GupMask = 0xE0;
  const char GlowMask = 0x1C;
  const char Bmask = 0xF8;
  
  // Set up string for trasnmission
  char string[3];
  string[0] = GSGC_BACKGND;

  // Combine the three byte RGB down to a two byte form
  // Byte1 = R4R3R2R1R0G5G4G3
  // Byte2 = G2G1G0B4B3B2B1B0
  string[1] = (R&Rmask) | ((G&GupMask)>>5);
  string[2] = ((G&GlowMask)<<3) | ((B&Bmask)>>3) ;

  // Send string and return the error result from transmit
  return transmit_string(string,3);
}

// Writes a string of text to the OLED
int write_text(char val){
  char string[8];

  string[0] = GSGC_STRINGTXT; //Send command header from text
  string[1] = 0x1;   //Set to column 1
  string[2] = 0x1;   //Set to row 1
  string[3] = FONT1;   //Set to 5x7 pixel font
  string[4] = (WHITE >> 8) & 0xFF;   //Set color to white 
  string[5] = WHITE & 0xFF;   //Set color to white part 2
  string[6] = val; //Send the count after converting
  string[7] = 0x00;   //NULL character terminates

  return transmit_string(string,8);
}

// Writes a string of text to the OLED that is null terminated
int write_string(char * val, int num){
  char string[30];
  int i;

  string[0] = GSGC_STRINGTXT; //Send command header from text
  string[1] = 0x1;   //Set to column 1
  string[2] = 0x1;   //Set to row 1
  string[3] = FONT1;   //Set to 5x7 pixel font
  string[4] = (WHITE >> 8) & 0xFF;   //Set color to white
  string[5] = WHITE & 0xFF;   //Set color to white part 2

  for(i = 0; i < num; i ++){
    string[i+6] = val[i];
  }

  return transmit_string(string,num+6);
}

//Removes everything except the background
int clearScreen(){
  return transmit_char(GSGC_CLS);
}

//Draws Alert rectangles along bottom of screen
//Section 1 is left, 2 is middle, 3 is right
int alert(char section){
  char string[7];  
  if(section & ALERTLEFT){  //Check if section includes left
    string[0] = GSGC_RECTANGLE;
    string[1] = 0; //Top left 'x' coordinate of rectangle
    string[2] = 123; //Top left 'y' coordinate of rectangle
    string[3] = 41; //Bottom right 'x' coordinate of rectangle
    string[4] = 127; //Bottom right 'y' coordinate of rectangle
    string[5] = (RED >> 8) & 0xFF;   //Set color to red
    string[6] = RED & 0xFF;   //Set color to red part 2
    transmit_string(string,7);
  }      
      
  if(section & ALERTMID){  //Check if section includes middle
    string[0] = GSGC_RECTANGLE;
    string[1] = 43;  //Top left 'x' coordinate of rectangle
    string[2] = 123; //Top left 'y' coordinate of rectangle
    string[3] = 84;  //Bottom right 'x' coordinate of rectangle
    string[4] = 127; //Bottom right 'y' coordinate of rectangle
    string[5] = (RED >> 8) & 0xFF;   //Set color to red
    string[6] = RED & 0xFF;   //Set color to red part 2
    transmit_string(string,7);
  }
           
  if(section & ALERTRIGHT){  //Check if section includes right
    string[0] = GSGC_RECTANGLE;
    string[1] = 86;  //Top left 'x' coordinate of rectangle
    string[2] = 123; //Top left 'y' coordinate of rectangle
    string[3] = 127; //Bottom right 'x' coordinate of rectangle
    string[4] = 127; //Bottom right 'y' coordinate of rectangle
    string[5] = (RED >> 8) & 0xFF;   //Set color to red
    string[6] = RED & 0xFF;   //Set color to red part 2
    transmit_string(string,7);
  }
  return 0;
}

//Removes alert rectangles by drawing a rectangle the same color as background
//must be passed background color as two formatted bytes
int unAlert(){
  return erase(0,123,127,127);
}

//Creates our custom bitmaps and adds them to Display's memory
int bitmaps(){
  //Make filled heart bitmap and place in index 0
  char string[10];
  string[0]=GSGC_ADDBM;
  string[1]=0;
  string[2]=0x66; //01100110
  string[3]=0xFF; //11111111
  string[4]=0xFF; //11111111
  string[5]=0xFF; //11111111
  string[6]=0xFF; //11111111
  string[7]=0x7E; //01111110
  string[8]=0x3C; //00111100
  string[9]=0x18; //00011000
  transmit_string(string, 10);
  
  //Make unfilled heart bitmap and place in index 1
  string[0]=GSGC_ADDBM;
  string[1]=1;
  string[2]=0x66; //01100110
  string[3]=0x99; //10011001
  string[4]=0x81; //10000001
  string[5]=0x81; //10000001
  string[6]=0x81; //10000001
  string[7]=0x42; //01000010
  string[8]=0x24; //00100100
  string[9]=0x18; //00011000
  transmit_string(string, 10);
  
  //Make left end of meter bitmap and place in index 2
  string[0]=GSGC_ADDBM;
  string[1]=2;
  string[2]=0x3F; //00111111
  string[3]=0x40; //01000000
  string[4]=0x80; //10000000
  string[5]=0x80; //10000000
  string[6]=0x80; //10000000
  string[7]=0x80; //10000000
  string[8]=0x40; //01000000
  string[9]=0x3F; //00111111
  transmit_string(string, 10);
  
  //Make middle of meter bitmap and place in index 3
  string[0]=GSGC_ADDBM;
  string[1]=2;
  string[2]=0xFF; //11111111
  string[3]=0x00; //00000000
  string[4]=0x00; //00000000
  string[5]=0x00; //00000000
  string[6]=0x00; //00000000
  string[7]=0x00; //00000000
  string[8]=0x00; //00000000
  string[9]=0xFF; //11111111
  transmit_string(string, 10);
  
  //Make right end of meter bitmap and place in index 4
  string[0]=GSGC_ADDBM;
  string[1]=4;
  string[2]=0xFC; //11111100
  string[3]=0x02; //00000010
  string[4]=0x01; //00000001
  string[5]=0x01; //00000001
  string[6]=0x01; //00000001
  string[7]=0x01; //00000001
  string[8]=0x02; //00000010
  string[9]=0xFC; //11111100
  transmit_string(string, 10);
  
  //Make filled left end of meter bitmap and place in index 5
  string[0]=GSGC_ADDBM;
  string[1]=5;
  string[2]=0x00; //00000000
  string[3]=0x3F; //00111111
  string[4]=0x7F; //01111111
  string[5]=0x7F; //01111111
  string[6]=0x7F; //01111111
  string[7]=0x7F; //01111111
  string[8]=0x3F; //00111111
  string[9]=0x00; //00000000
  transmit_string(string, 10);
  
  //Make filled middle of meter bitmap and place in index 6
  string[0]=GSGC_ADDBM;
  string[1]=6;
  string[2]=0x00; //00000000
  string[3]=0xFF; //11111111
  string[4]=0xFF; //11111111
  string[5]=0xFF; //11111111
  string[6]=0xFF; //11111111
  string[7]=0xFF; //11111111
  string[8]=0xFF; //11111111
  string[9]=0x00; //00000000
  transmit_string(string, 10);
  
  //Make filled right end of meter bitmap and place in index 7
  string[0]=GSGC_ADDBM;
  string[1]=7;
  string[2]=0x00; //00000000
  string[3]=0xFC; //11111100
  string[4]=0xFE; //11111110
  string[5]=0xFE; //11111110
  string[6]=0xFE; //11111110
  string[7]=0xFE; //11111110
  string[8]=0xFC; //11111100
  string[9]=0x00; //00000000
  transmit_string(string, 10);
  
  //Make Ping connectivity bitmap and place in index 8
  string[0]=GSGC_ADDBM;
  string[1]=8;
  string[2]=0x54; //01010100
  string[3]=0x2A; //00101010
  string[4]=0x2A; //00101010
  string[5]=0x15; //00010101
  string[6]=0x15; //00010101
  string[7]=0x2A; //00101010
  string[8]=0x2A; //00101010
  string[9]=0x54; //01010100
  transmit_string(string, 10);
  
  //Make Wheel connectivity bitmap and place in index 9
  string[0]=GSGC_ADDBM;
  string[1]=9;
  string[2]=0x3C; //00111100
  string[3]=0x42; //01000010
  string[4]=0xA5; //10100101
  string[5]=0x99; //10011001
  string[6]=0x99; //10011001
  string[7]=0xA5; //10100101
  string[8]=0x42; //01000010
  string[9]=0x3C; //00111100
  return transmit_string(string, 10);
}



//Erases a rectangle area by setting it to same color as background
int erase(char x,char y, char endX, char endY){
  char string[7];
  
  string[0] = GSGC_RECTANGLE;
  string[1] = x;  //Top left 'x' coordinate of rectangle
  string[2] = y; //Top left 'y' coordinate of rectangle
  string[3] = endX; //Bottom right 'x' coordinate of rectangle
  string[4] = endY; //Bottom right 'y' coordinate of rectangle
  string[5] = (BACKGROUND>>8) & 0xFF;  //Set RG portion of background color
  string[6] = BACKGROUND & 0xFF; //Set GB portion of background color

  return transmit_string(string,7);
}

/**********************************************************
 * The format functions take a pointer to a string and the 
 * numbers to be formatted and place the numbers in a string, 
 * string size should be allocated by calling function
 *********************************************************/

void format_time(char* string, int hour, int min, int sec){
	string[0]=(hour/10)+0x30;
	string[1]=(hour%10)+0x30;
	string[2]=':';
	string[3]=(min/10)+0x30;
	string[4]=(min%10)+0x30;
	string[5]=':';
	string[6]=(sec/10)+0x30;
	string[7]=(sec%10)+0x30;
	string[8]=0x0;
	return;
}

void format_heartRate(char* string, int hbpm){
	string[0]=(hbpm/100)?(hbpm/100)+0x30:' ';
	string[1]=(hbpm/10)?((hbpm/10)%10)+0x30:' ';
	string[2]=(hbpm%10)+0x30;
	string[3]=0x0;
	return;
}

//spd is 3 digit raw data with lsd being tenths fractional
void format_speed(char* string, int spd){
	string[0]=(spd/100)?(spd/100)+0x30:' ';
	string[1]=(spd/10)?((spd/10)%10)+0x30:'0';
	string[2]='.';
	string[3]=(spd%10)+0x30;
	string[4]=0x0;
	return;
}

//dist is 4 digit raw data with lsd being hundreths fractional
void format_distance(char * string, int dist){
	string[0]=(dist/1000)?(dist/1000)+0x30:' ';
	string[1]=(dist/100)?((dist/100)%10)+0x30:'0';
  string[2]='.';
	string[3]=(dist/10)?((dist/10)%10)+0x30:'0';
	string[4]=(dist%10)+0x30;
	string[5]=0x0;
	return;
}

//cal is 3 digit whole number
void format_calories(char * string, int cal){
	string[0]=(cal/100)?(cal/100)+0x30:' ';
	string[1]=(cal/10)?((cal/10)%10)+0x30:' ';
	string[2]=(cal%10)+0x30;
	string[3]=0x0;
	return;
}

//cadence is 3 digit whole number
void format_cadence(char * string, int cad){
	string[0]=(cad/100)?(cad/100)+0x30:' ';
	string[1]=(cad/10)?((cad/10)%10)+0x30:' ';
	string[2]=(cad%10)+0x30;
	string[3]=0x0;
	return;
}
/**********************************************************
 * SubScreen - the subscreen functions are called to write a
 * string to a given subscreen
 * *******************************************************/

//length is length of string, not including null terminator
//Max string size is 31;
int subScreen(int startX, int startY, char* string, int length, int font, int scale){
	char command[40]; //max string size plus 9
	int i;
	
	command[0]=GSGC_STRINGGFX;
	command[1]=startX;
	command[2]=startY;
	command[3]=font;
	command[4]=(TXTCOLOR>>8) & 0xFF;
	command[5]=TXTCOLOR & 0xFF;
	command[6]=scale;
	command[7]=scale+1;
	for(i=0;i<length+1;i++){
		command[i+8]=string[i];
	}
	return transmit_string(command,length+9);
}

//Top subScreen will use 8x12 font, scaled up to 16x36, length is length of string not counting null terminator
int top_subScreen(char* string, int length){
	int startX = (128 - length*16)/2;
	int startY = 13;
	int font = 2;
	int scale = 2;
	return subScreen(startX,startY,string,length,font,scale);
}

//Both bottom subScreens will use 5x7 font, scaled up to 10x21, length is length of string not counting null terminator
int botLeft_subScreen(char* string, int length){
	int startX = 0;
	int startY = 78;
	int font = 0;
	int scale = 2;
	return subScreen(startX,startY,string,length,font,scale);
}

int botRight_subScreen(char* string, int length){
  int startX = 62;
	//int startX = 124 - length*10;
	int startY = 78;
	int font = 0;
	int scale = 2;
	return subScreen(startX,startY,string,length,font,scale);
}


/**********************************************************
 * Label functions - The label functions write the
 * appropriate label to each of the subscreens or the graphic
 * that goes with the subscreen
 * *******************************************************/

//labels use 8x8 font with no scaling
int labelsTxt(char* label1, int length1, char* label2, int length2, char* label3, int length3){
	char command[18];
	int i;
	
	command[0]=GSGC_STRINGGFX;
	command[1]=8;
	command[2]=0;
	command[3]=1;
	command[4]=(TXTCOLOR>>8) & 0xFF;
	command[5]=TXTCOLOR & 0xFF;
	command[6]=1;
	command[7]=1;
	for(i=0;i<length1+1;i++){
		command[i+8]=label1[i];
	}
	transmit_string(command,length1+9);
	
	command[0]=GSGC_STRINGGFX;
	command[1]=8;
	command[2]=62;
	command[3]=1;
	command[4]=(TXTCOLOR>>8) & 0xFF;
	command[5]=TXTCOLOR & 0xFF;
	command[6]=1;
	command[7]=1;
	for(i=0;i<length2+1;i++){
		command[i+8]=label2[i];
	}
	transmit_string(command,length2+9);
	
	command[0]=GSGC_STRINGGFX;
	command[1]=72;
	command[2]=62;
	command[3]=1;
	command[4]=(TXTCOLOR>>8) & 0xFF;
	command[5]=TXTCOLOR & 0xFF;
	command[6]=1;
	command[7]=1;
	for(i=0;i<length3+1;i++){
		command[i+8]=label3[i];
	}
	transmit_string(command,length3+9);

	return 0;
}

//label1 is for top subscreen, label2 bottom left, label3 bottom right, pass index of bitmap+1, 0 for none
int labelsGfx(int label1, int label2, int label3){
	char command[6];
	
	if(label1){
		command[0]=GSGC_BM;
		command[1]=label1-1;
		command[2]=0;
		command[3]=0;
		command[4]=(RED>>8)&0xFF;
		command[5]=RED&0xFF;
		transmit_string(command,6);
	}
	
	if(label2){
		command[0]=GSGC_BM;
		command[1]=label2-1;
		command[2]=0;
		command[3]=62;
		command[4]=(RED>>8)&0xFF;
		command[5]=RED&0xFF;
		transmit_string(command,6);
	}
	
	if(label3){
		command[0]=GSGC_BM;
		command[1]=label3-1;
		command[2]=64;
		command[3]=62;
		command[4]=(RED>>8)&0xFF;
		command[5]=RED&0xFF;
		transmit_string(command,6);
	}
	return 0;
}

//Connectivity displays a grey icon for a disconnected sensor and white for connected
//uses bitmaps unfilled heart for heart-rate, wheel for cadence, ping for proximity 
int connectivity(char flags){
	char command[6];
	
	command[0]=GSGC_BM;
	command[1]=1;
	command[2]=100;
	command[3]=0;
	if(flags&CONNHR){
		command[4]=(WHITE>>8)&0xFF;
		command[5]=WHITE&0xFF;
	}
	else{
		command[4]=(GRAY>>8)&0xFF;
		command[5]=GRAY&0xFF;
	}
	transmit_string(command,6);
	
	command[0]=GSGC_BM;
	command[1]=9;
	command[2]=110;
	command[3]=0;
	if(flags&CONNCAD){
		command[4]=(WHITE>>8)&0xFF;
		command[5]=WHITE&0xFF;
	}
	else{
		command[4]=(GRAY>>8)&0xFF;
		command[5]=GRAY&0xFF;
	}
	transmit_string(command,6);
	
	command[0]=GSGC_BM;
	command[1]=8;
	command[2]=120;
	command[3]=0;
	if(flags&CONNPROX){
		command[4]=(WHITE>>8)&0xFF;
		command[5]=WHITE&0xFF;
	}
	else{
		command[4]=(GRAY>>8)&0xFF;
		command[5]=GRAY&0xFF;
	}
	transmit_string(command,6);
	
	return 0;
}
/**********************************************************
 * Screen Functions - These are the functions that should be
 * called to write the bikers data to the screen. The screen
 * number specifies what data is being printed
 * *******************************************************/

 //Screen 1 prints time, current speed, and distance
int screen1(int hour, int min, int sec, int spdCur, int distRaw, char flags){
	char time[9];
	format_time(time,hour,min,sec);
	char curSpd[5];
	format_speed(curSpd,spdCur);
	char dist[6];
	format_distance(dist,distRaw);
	
	char label1[] = "time";
	char label2[] = "spd";
	char label3[] = "dist mi";
	
	clearScreen();
	top_subScreen(time,8);
	botLeft_subScreen(curSpd,4);
	botRight_subScreen(dist,5);
	
	labelsTxt(label1,4,label2,3,label3,7);
	labelsGfx(0,0,0);
	alert(flags);
	connectivity(flags);

	return 0;
}

//Screen 2 prints time, current heart rate and calories burned
int screen2(int hour, int min, int sec, int hbpmCur, int calRaw, char flags){
	char time[9];
	format_time(time,hour,min,sec);
	char curHR[4];
	format_heartRate(curHR,hbpmCur);
	char cal[4];
	format_calories(cal,calRaw);
	
	char label1[] = "time";
	char label2[] = " ";
	char label3[] = "Cal";
	
	clearScreen();
	top_subScreen(time,8);
	botLeft_subScreen(curHR,3);
	botRight_subScreen(cal,3);
	
	labelsTxt(label1,4,label2,1,label3,3);
	labelsGfx(0,(sec%2)+1,0);
	alert(flags);
	connectivity(flags);

	return 0;
}

//Screen 3 prints time, cadence, and distance
int screen3(int hour, int min, int sec, int cadRaw, int distRaw, char flags){
	char time[9];
	format_time(time,hour,min,sec);
	char cad[4];
	format_cadence(cad,cadRaw);
	char dist[6];
	format_distance(dist,distRaw);
	
	char label1[] = "time";
	char label2[] = "cadence";
	char label3[] = "dist mi";
	
	clearScreen();
	top_subScreen(time,8);
	botLeft_subScreen(cad,3);
	botRight_subScreen(dist,5);
	
	labelsTxt(label1,4,label2,7,label3,7);
	labelsGfx(0,0,0);
	alert(flags);
	connectivity(flags);

	return 0;
}

//Screen 4 prints current heart rate, current speed, and cadence
int screen4(int sec, int hbpmCur, int spdRaw, int cadRaw, char flags){
	char curHR[4];
	format_heartRate(curHR,hbpmCur);
	char curSpd[5];
	format_speed(curSpd,spdRaw);
	char cad[4];
	format_cadence(cad,cadRaw);
	
	char label1[] = " ";
	char label2[] = "spd";
	char label3[] = "cadence";
	
	clearScreen();
	top_subScreen(curHR,3);
	botLeft_subScreen(curSpd,4);
	botRight_subScreen(cad,3);
	
	labelsTxt(label1,1,label2,3,label3,7);
	labelsGfx((sec%2)+1,0,0);
	alert(flags);
	connectivity(flags);

	return 0;
}

//Screen 5 prints average heart rate, average speed, and average cadence
int screen5(int sec, int hbpmAvg, int spdAvg, int cadAvg, char flags){
	char avgHR[4];
	format_heartRate(avgHR,hbpmAvg);
	char avgSpd[5];
	format_speed(avgSpd,spdAvg);
	char avgCad[4];
	format_cadence(avgCad,cadAvg);
	
	char label1[] = " avg";
	char label2[] = "avg spd";
	char label3[] = "avg cad";
	
	clearScreen();
	top_subScreen(avgHR,3);
	botLeft_subScreen(avgSpd,4);
	botRight_subScreen(avgCad,3);
	
	labelsTxt(label1,4,label2,7,label3,7);
	labelsGfx((sec%2)+1,0,0);
	alert(flags);
	connectivity(flags);
	
	return 0;
}

//Screen 6 prints max heart rate, max speed, and max cadence
int screen6(int sec, int hbpmMax, int spdMax, int cadMax, char flags){
	char maxHR[4];
	format_heartRate(maxHR,hbpmMax);
	char maxSpd[5];
	format_speed(maxSpd,spdMax);
	char maxCad[4];
	format_cadence(maxCad,cadMax);
	
	char label1[] = " max";
	char label2[] = "max spd";
	char label3[] = "max cad";
	
	clearScreen();
	top_subScreen(maxHR,3);
	botLeft_subScreen(maxSpd,4);
	botRight_subScreen(maxCad,3);
	
	labelsTxt(label1,4,label2,7,label3,7);
	labelsGfx((sec%2)+1,0,0);

	alert(flags);
	connectivity(flags);
	
	return 0;
}
